package com.niiwoo.civet.user.service.duboo.asset;

import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.user.dto.request.asset.CardniuCreditCheckRequestDTO;
import com.niiwoo.civet.user.enums.ProductTypeEnum;
import com.niiwoo.civet.user.service.asset.CardniuUserDubboService;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description:
 * @Author:LiJian
 * @Date:2018/2/13
 */
@Slf4j
@Service(version = "1.0.0")
public class CardniuUserDubboServiceImpl implements CardniuUserDubboService {
    @Override
    public void cardniuCreditAllowCheck(CardniuCreditCheckRequestDTO creditCheckRequestDTO) {

    }

    @Override
    public Boolean getUserLibraLoanLimit(String userId, ProductTypeEnum productTypeEnum) {
        return null;
    }
/*
    @Autowired
    private UserBlackListLocalService userBlackListLocalService;

    @Autowired
    private LibraLoanLimitService libraLoanLimitService;

    @Autowired
    private CardniuUserService cardniuUserService;

    @Autowired
    private UserForbidService userForbidService;

    @Autowired
    private PrivacyMasks privacyMasks;

    @Reference(version = "1.0.0")
    private TradeConfigDubboService tradeConfigDubboService;

    public static final DateTimeFormatter format = DateTimeFormat.forPattern("yyyyMMdd");

    @Override
    public void cardniuCreditAllowCheck(CardniuCreditCheckRequestDTO creditCheckRequestDTO) {
        log.info("cardniuUserCreditAdmitCheck，入参={}", JSON.toJSONString(creditCheckRequestDTO));

        // 手机号黑灰名单检测
        List<UserBlackList> userBlackList = null;
        if(StringUtils.isNotBlank(creditCheckRequestDTO.getMobileNo())) {
            userBlackList = userBlackListLocalService.getUserBlackListByMobileNo(privacyMasks.encryptPrivacy(creditCheckRequestDTO.getMobileNo()));
            if(CollectionUtils.isNotEmpty(userBlackList)) {
                log.warn("卡牛用户手机号={}，命中黑灰名单", creditCheckRequestDTO.getMobileNo());
                throw new CardNiuBizException("NKS1013");
            }
        }
        if(StringUtils.isNotBlank(creditCheckRequestDTO.getIdCard())) {
            //年龄限制检测
            String idCard = creditCheckRequestDTO.getIdCard();
            int age = IdCardUtils.getAgeByIdCard(idCard);
            List<TradeConfigEnum> enumList = Arrays.asList(TradeConfigEnum.CARDNIU_MINACCESSAGE, TradeConfigEnum.CARDNIU_MAXACCESSAGE);
            Map<String, String> map = tradeConfigDubboService.selectTradeConfigByEmuns(enumList);
            if ((map.containsKey(TradeConfigEnum.CARDNIU_MINACCESSAGE.getConfigKey()) && Integer.parseInt(map.get(TradeConfigEnum.CARDNIU_MINACCESSAGE.getConfigKey())) > age) ||
                    (map.containsKey(TradeConfigEnum.CARDNIU_MAXACCESSAGE.getConfigKey()) && Integer.parseInt(map.get(TradeConfigEnum.CARDNIU_MAXACCESSAGE.getConfigKey())) < age) ){
                log.warn("卡牛用户idCard={}，年龄不在借款限制年龄范围", creditCheckRequestDTO.getIdCard());
                throw new CardNiuBizException("NKS1011");
            }

            // 身份证有效期检测
            if(StringUtils.isNotBlank(creditCheckRequestDTO.getExpiredDate())) {
                DateTime idcardExpiredTime = DateTime.parse(creditCheckRequestDTO.getExpiredDate(), format);
                if(idcardExpiredTime.isBeforeNow() || idcardExpiredTime.minusDays(30).isBeforeNow()) {
                    log.warn("卡牛用户idCard={}，身份证已过期或距过期少于等于1个月", creditCheckRequestDTO.getIdCard());
                    throw new CardNiuBizException("NKS1012");
                }
            }
            // 身份证黑灰名单
            userBlackList = userBlackListLocalService.getUserBlackListByIdCard(privacyMasks.encryptPrivacy(idCard));
            if(CollectionUtils.isNotEmpty(userBlackList)) {
                log.warn("卡牛用户idCard={}，命中黑灰名单", creditCheckRequestDTO.getIdCard());
                throw new CardNiuBizException("NKS1013");
            }
        }
        // 借款行为限制
        if(StringUtils.isNotBlank(creditCheckRequestDTO.getUserId())) {
            ValidUserForbidResponseDTO validUserForbidResponseDTO = userForbidService.queryValidUserForbidInfo(creditCheckRequestDTO.getUserId(), UserForbidTypeEnum.SPEEDBORROWED);
            if(validUserForbidResponseDTO != null) {
                throw new CardNiuBizException("NKS1015");
            }
        }

        // 借款限制检测
        if(StringUtils.isNotBlank(creditCheckRequestDTO.getUserId())) {
            UserLibraLoanLimit userLibraLoanLimit = libraLoanLimitService.getUserLibraLoanLimit(creditCheckRequestDTO.getUserId(), ProductTypeEnum.CARD_NIU_LOAN);
            if(userLibraLoanLimit != null) {
                LibraLoanLimitEnum libraLoanLimitEnum = LibraLoanLimitEnum.getEnumByType(userLibraLoanLimit.getLimitType());
                log.warn("卡牛用户userId={}, 命中借款限制", creditCheckRequestDTO.getUserId());
                if(libraLoanLimitEnum.equals(LibraLoanLimitEnum.CREDIT_AMOUNT_LESS)) {
                    throw new CardNiuBizException("NKS1014");
                }else if(libraLoanLimitEnum.equals(LibraLoanLimitEnum.CREDIT_AGAINST_FAKE)) {
                    throw new CardNiuBizException("NKS1016");
                }
            }
        }

        String userId = cardniuUserService.getUserIdByMobileNo(creditCheckRequestDTO.getMobileNo());
        String cardNiuUserId = creditCheckRequestDTO.getUserId();
        // 手机号是否注册检测
        if(StringUtils.isNoneBlank(userId, cardNiuUserId) && !cardNiuUserId.equals(userId)) {
            log.warn("卡牛用户mobileNo={}, 手机号已注册", creditCheckRequestDTO.getMobileNo());
            throw new CardNiuBizException("NKS1001");
        }
        // 身份证号是否已存在检测
        if(StringUtils.isBlank(userId) && StringUtils.isNotBlank(creditCheckRequestDTO.getIdCard())) {
            if(StringUtils.isNotBlank(cardniuUserService.getUserIdByIdCard(creditCheckRequestDTO.getIdCard()))) {
                log.warn("卡牛用户userId={}, 身份证已存在", creditCheckRequestDTO.getIdCard());
                throw new CardNiuBizException("NKS1001");
            }
        }
        // 实名信息和平台对应信息检测
        if(StringUtils.isNotBlank(userId)) {
            if (!cardniuUserService.checkIdentityInfo(userId, creditCheckRequestDTO.getRealName(), creditCheckRequestDTO.getIdCard())) {
                log.warn("卡牛用户realName={}, idCard={}, 身份信息不一致", creditCheckRequestDTO.getRealName(), creditCheckRequestDTO.getIdCard());
                throw new CardNiuBizException("NKS1001");
            }
        }

    }

    @Override
    public Boolean getUserLibraLoanLimit(String userId, ProductTypeEnum productTypeEnum){
        UserLibraLoanLimit userLibraLoanLimit = libraLoanLimitService.getUserLibraLoanLimit(userId, productTypeEnum);
        if(userLibraLoanLimit != null){
            return true;
        }
        return false;
    }*/

}
